/**
 *  * Bestpay.com.cn Inc.
 *  * Copyright (c) 2011-2017 All Rights Reserved.
 *  
 */
package Test;

import com.dangdang.ddframe.job.reg.exception.RegExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
  * 测试curator操作zookeeper
  * 测试 client不关闭一旦启动会自动发起重连
  * @author yedengfeng
  * @version Id: TestCurator.java, v 0.1 2017/5/15 11:34 yedengfeng Exp $$
  */
//@Component
@Slf4j
public class TestCurator implements Testcase{

    @Override
//    @PostConstruct
    public void runTest()  {
        Thread n = new Thread(new Runnable() {
            @Override
            public void run() {
                RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
                CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181",retryPolicy);
                client.start();
                try {
//                    client.blockUntilConnected(9000, TimeUnit.MILLISECONDS);
//                    if(!client.getZookeeperClient().isConnected()) {
//                        throw new KeeperException.OperationTimeoutException();
//                    }
                }catch (Exception var3) {
                    RegExceptionHandler.handleException(var3);
                }
            }
        });
        n.start();
//        try {
////            add(client);
//            delete(client);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public  void add(CuratorFramework client ) throws Exception {
        client.create().withMode(CreateMode.PERSISTENT).forPath("/zk");
        client.close();
    }

    public  void delete(CuratorFramework client ) throws Exception {
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk1");
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk2");
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk3");
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk4");
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk5");
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk6");
        client.delete().deletingChildrenIfNeeded().forPath("/zookeeper/zk7");

        client.close();
    }

    public  void update(CuratorFramework client ){

    }

    public  void get(CuratorFramework client ){

    }

}
